﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PBOffice.Models;
using PBOffice.BLL;

public partial class PersonalManager_AddStaff : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
      //  this.txtDept.Attributes.Add("onclick", "javascript:OpenWin(250,400)");
        if (!IsPostBack)
        {

            if (Request.QueryString["id"] != null)
            {
                int stfId = Convert.ToInt32(Request.QueryString["id"]);
                Staff staff = new StaffManager().GetStaffByStfId(stfId);
                this.txtName.Text = staff.StfName;
                this.txtCode.Text = staff.StfCode;
                this.txtDept.Value = staff.Department.DeptName;
                this.position.Value = staff.Position.PosName;
                this.ddlSuper.Value = staff.SuperId.ToString();
                this.ddlEducation.Text = staff.Education;
                this.txtAddress.Text = staff.Address;
                this.txtBornDate.Text = staff.BornDate.ToShortDateString();
                this.txtMobileTelNo.Text = staff.MobilePhone;
                this.txtEmail.Text = staff.Mail;
                this.txtTelephone.Text = staff.TelePhone;
                this.txtZip.Text = staff.Zip;
                if (staff.Gender == true)
                {
                    this.rbMan.Checked = true;
                }
                else
                {
                    this.rbWoman.Checked = true;
                }


            }
        }


    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (CheckEmpty() == true)
        {
            Staff staff = new Staff();
            staff.Address = this.txtAddress.Text.Trim();
            staff.BornDate = Convert.ToDateTime(this.txtBornDate.Text.Trim());
            Department dept = new Department();
            dept.DeptId = new DeptManager().GetIdByDeptName(this.txtDept.Value.Trim());
            dept.DeptName = this.txtDept.Value.Trim();
            staff.Department = dept;
            staff.Education = this.ddlEducation.Text.Trim();
            staff.JoinDate = Convert.ToDateTime(this.txtJoinDate.Text.Trim());
            staff.Mail = this.txtEmail.Text.Trim();
            staff.MobilePhone = this.txtMobileTelNo.Text.Trim();
            Position pos = new Position();
            pos.PosName = this.position.Value.Trim();
            pos.PosId = new PosManager().GetPosIdByName(this.position.Value.Trim());
            staff.Position = pos;
            staff.StfCode = this.txtCode.Text.Trim();
            staff.StfName = this.txtName.Text.Trim();
            staff.SuperId = Convert.ToInt32(this.ddlSuper.Value);
            staff.TelePhone = txtTelephone.Text.Trim();
            staff.TelePoneExt = txtTelephoneExp.Text.Trim();
            staff.WorkExp = txtWorkExp.Text.Trim();
            staff.Zip = txtZip.Text.Trim();
            if (this.rbMan.Checked == true)
            {
                staff.Gender = true;
            }
            if (this.rbWoman.Checked == true)
            {
                staff.Gender = false;
            }
            if (Request.QueryString["id"] != null)
            {
                staff.Password = new StaffManager().GetStaffByStfId(Convert.ToInt32(Request.QueryString["id"])).Password;

                staff.Status = new StaffManager().GetStaffByStfId(Convert.ToInt32(Request.QueryString["id"])).Status;

                staff.StfId = Convert.ToInt32(Request.QueryString["id"]);

                new StaffManager().UpdateStaff(staff);
            }
            else
            {
                staff.Password = "1234";


                staff.Status = 0;


                new StaffManager().AddStaff(staff);
            }
        }

    }
    private bool CheckEmpty()
    {
        if (this.txtCode.Text == "")
        {
            Page.ClientScript.RegisterClientScriptBlock(typeof(string), "alert", "<script>alert('员工编号不能为空！')</script>");
            return false;
        }
        if (this.txtName.Text == "")
        {
            Page.ClientScript.RegisterClientScriptBlock(typeof(string), "alert", "<script>alert('部门名称不能为空！')</script>");
            return false;
        }
        if (this.txtDept.Value == "")
        {
            Page.ClientScript.RegisterClientScriptBlock(typeof(string), "alert", "<script>alert('员工编号不能为空！')</script>");
            return false;
        }
        //if (this.ddlPosition. == "请选择")
        //{
        //    Page.ClientScript.RegisterClientScriptBlock(typeof(string), "alert", "<script>alert('请选择职位！')</script>");
        //    return false;
        //}
        //if (this.ddlSuper.Text == "请选择")
        //{
        //    Page.ClientScript.RegisterClientScriptBlock(typeof(string), "alert", "<script>alert('请选择直接上级！')</script>");
        //    return false;
        //}
        if (this.ddlEducation.Text == "请选择")
        {
            Page.ClientScript.RegisterClientScriptBlock(typeof(string), "alert", "<script>alert('请选择学历！')</script>");
            return false;
        }
        if (this.txtBornDate.Text == "")
        {
            Page.ClientScript.RegisterClientScriptBlock(typeof(string), "alert", "<script>alert('出生年月不能为空！')</script>");
            return false;
        }
        if (this.txtJoinDate.Text == "")
        {
            Page.ClientScript.RegisterClientScriptBlock(typeof(string), "alert", "<script>alert('参加工作的日期不能为空！')</script>");
            return false;
        }
        if (this.txtMobileTelNo.Text == "")
        {
            Page.ClientScript.RegisterClientScriptBlock(typeof(string), "alert", "<script>alert('手机号不能为空！')</script>");
            return false;
        }
        if (this.rbMan.Checked == false && this.rbWoman.Checked == false)
        {
            Page.ClientScript.RegisterClientScriptBlock(typeof(string), "alert", "<script>alert('您还没有选择性别！')</script>");
            return false;

        }
        return true;
    }
    protected void btnCancle_Click(object sender, EventArgs e)
    {
        Response.Redirect("Staff.aspx");
    }
}